package com.zheng.cms.admin.activemq.controller;

import com.zheng.cms.admin.activemq.service.ConsumerService;
import com.zheng.cms.admin.activemq.service.ProducerService;
import com.zheng.cms.admin.activemq.service.QueueProducer;
import com.zheng.cms.admin.activemq.service.TopicProducer;
import com.zheng.cms.common.constant.CmsResult;
import com.zheng.cms.common.constant.CmsResultConstant;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.Destination;


/**
 *  订阅模式有区别，如果做到想微信一样提取最近的消息订阅，
 *  消息订阅叠加之后会溢出，所以需要打到想要的效果，需要对底层源码进行修改。
 *  订阅模式在加个模式，对于超过7天的消息，服务器不提供缓存。
 * */
@Controller
@Api(value = "ActiveMQ", description = "测试")
@RequestMapping("/manage/activemq")
public class TestController {

    /**定义的消息队列，一对一*/
    @Resource(name = "defaultQueueDestination")
    private Destination defaultQueueDestination;
    /**一对多*/
    @Resource(name = "defaultTopicDestination")
    private Destination defaultTopicDestination;

    @Autowired
    private ProducerService producer;
    @Autowired
    private ConsumerService consumer;
    @Autowired
    private QueueProducer queueProducer;
    @Autowired
    private TopicProducer topicProducer;

//    /**跳转到消息发送界面*/
//    @RequestMapping(value = "/producer")
//    public String producer(){
//
//        System.out.println("--------------go producer");
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = sdf.format(date);
//
//
//        return "/manage/activemq/producer.jsp";
//    }
//
//    /**一对多送消息*/
//    @RequestMapping(value = "/mulitProducer")
//    public Object producer(String msg){
//
//        System.out.println("---------send to jms");
//        ModelAndView mv = new ModelAndView();
//        producer.sendMessage(defaultQueueDestination, msg);
//
//        return new CmsResult(CmsResultConstant.SUCCESS, "发送成功");
//    }
//
//    /**发送消息*/
//    @ResponseBody
//    @RequestMapping(value = "/onsend")
//    public Object send(String msg){
//        System.out.println("---------send to jms:"+msg);
//        producer.sendMessage(defaultTopicDestination, "test");
//        return new CmsResult(CmsResultConstant.SUCCESS, "发送成功");
//    }
//
//    /**接收消息*/
//    @ResponseBody
//    @RequestMapping(value="/receive")
//    public Object queue_receive() throws JMSException {
//        System.out.println("------------receive message");
//        TextMessage tm = consumer.receive(defaultQueueDestination);
//        System.out.println(tm.getText());
//        return  new CmsResult(CmsResultConstant.SUCCESS, "接收成功:"+tm.getText());
//    }



    /**发送消息
     *  http://cms.zhangshuzheng.cn:2222/manage/activemq/queueSend
     *
     *  Listerner可以接收到消息了，zheng的activemq没写好啊。
     * ---------send to jms:null
     * 接收到消息：----------------- queueSend ------
     *
     *
     * ---------send to jms:null
     * qtp2141739605-16 向队列topic://com.zheng.cms.admin.topic.default发送消息---------------------->test
     * 配置了2个点对锦亭器的时候，2个监听器都没有接收到消息，说明只能点对点模式。,
     * 改成一个点对点配置的时候，监听到消息了。
     * */
    @ResponseBody
    @RequestMapping(value = "/queueSend")
    public Object queueSend(String msg){
        System.out.println("---------send to jms:"+msg);
        queueProducer.sendTextMessage("----------------- queueSend ------");
        return new CmsResult(CmsResultConstant.SUCCESS, "发送成功");
    }




    /**发送消息
     *  http://cms.zhangshuzheng.cn:2222/manage/activemq/topicSend
     *
     *  Listerner可以接收到消息了，zheng的activemq没写好啊。
     * ---------send to jms:null

     *
     * consumer写了2个监听器，出现了2调数据,
     *
     *  MyMessageListener,接收到消息：----------------- topicSend ------
     *  MyMessageListener,接收到消息：----------------- topicSend ------
     *
     *  MyMessageListener既写成了点对点又写成了订阅模式。点对点和订阅模式的区别了？
     *
     * */
    @ResponseBody
    @RequestMapping(value = "/topicSend")
    public Object topicSend(String msg){
        System.out.println("---------send to jms:topicSend");
        topicProducer.sendTextMessage("----------------- topicSend ------");
        return new CmsResult(CmsResultConstant.SUCCESS, "发送成功");
    }
}
